package com.xiaoqian.partyembassy.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;

import com.xiaoqian.partyembassy.R;
import com.xiaoqian.partyembassy.tools.CountTimeMachines;


/**
 *
 */
public class DialogLoadingView extends Dialog {
	public DialogLoadingView(Context context) {
		super(context);
	}
	public DialogLoadingView(Context context, boolean cancelable,
                             OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public DialogLoadingView(Context context, int theme) {
		super(context, theme);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.dialog_loading_view);
		getWindow().setBackgroundDrawableResource(android.R.color.transparent);
		getWindow().setDimAmount(0f);
		this.setCanceledOnTouchOutside(false);
	}
	@Override
	protected void onStart() {
		super.onStart();
		startTimer();
	}


	@Override
	protected void onStop() {
		super.onStop();
		pauseCountDown();
	}
	public void refreshTime(){
		heatIndex = 0;
	}
	/**
	 * 暂停定时
	 */
	public void pauseCountDown() {
		heatIndex = 0;
		if (countTimeMachines != null) {
			countTimeMachines.stopTime();
		}
	}
	/**
	 * 开始定时任务
	 */
	private void startTimer() {
		heatIndex = 0;
		if (countTimeMachines != null) {
			countTimeMachines.starTime();
		}
	}



	private int heatIndex;
	CountTimeMachines countTimeMachines = new CountTimeMachines() {
		@Override
		public void onTick() {
			heatIndex++;
			if (heatIndex > 120) {
				dismiss();
				heatIndex = 0;
			}
		}
	};
}
